package com.rchat.pocmini.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeContactPresence;
import com.airtalkee.sdk.entity.AirContact;
import com.rchat.pocmini.R;
import com.rchat.pocmini.account.PocAccount;
import com.rchat.pocmini.activity.bean.GroupMemberBean;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/3/8.
 */

public class GroupMemberAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<GroupMemberBean> data;
    private Context context;

    public GroupMemberAdapter(Context context, ArrayList<GroupMemberBean> data) {
        this.data = data;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.group_member_list_view_item_layout, null);
            holder = new ViewHolder();
            holder.ivMemberAvatar = (ImageView) convertView.findViewById(R.id.iv_member_avatar);
            holder.ivInChat = (ImageView) convertView.findViewById(R.id.iv_in_chat);
            holder.tvMemberName = (TextView) convertView.findViewById(R.id.tv_member_name);
            holder.tvMemberId = (TextView) convertView.findViewById(R.id.tv_member_id);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final GroupMemberBean bean = data.get(position);

        //不显示ipoc id 了
        holder.tvMemberId.setText("");
        holder.tvMemberName.setText(bean.getDisplayName());

        switch (AirtalkeeContactPresence.getInstance().getContactStateById(bean.getUserName())) {

            case AirContact.CONTACT_STATE_ONLINE:
                holder.ivMemberAvatar.setImageResource(R.drawable.member_head_online);
                holder.ivInChat.setVisibility(View.VISIBLE);
//                if (bean.getStateInChat() == AirContact.IN_CHAT_STATE_ONLINE){
//                    holder.ivInChat.setVisibility(View.VISIBLE);
//                }else {
//                     holder.ivInChat.setVisibility(View.GONE);
//                }
                holder.ivInChat.setVisibility(View.GONE);
                break;
            default:
                holder.ivMemberAvatar.setImageResource(R.drawable.member_head_offline);
                holder.ivInChat.setVisibility(View.GONE);
        }
        if (bean.getUserName().equals(PocAccount.getUserId())) {//自己强制在线
            holder.ivMemberAvatar.setImageResource(R.drawable.member_head_online);
            holder.ivInChat.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private static class ViewHolder {
        private ImageView ivMemberAvatar;
        private ImageView ivInChat;
        private TextView tvMemberName;
        private TextView tvMemberId;
    }

    public ArrayList<GroupMemberBean> getData() {
        return this.data;
    }
}
